﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{/// <summary>
/// 罗马数字转整数
/// </summary>
    public class Solution24
    {
        public int RomanToInt(string s)
        {
            Dictionary<char, int> dictionry = new Dictionary<char, int>();
            dictionry.Add('I', 1);
            dictionry.Add('V', 5);
            dictionry.Add('X', 10);
            dictionry.Add('L', 50);
            dictionry.Add('C', 100);
            dictionry.Add('D', 500);
            dictionry.Add('M', 1000);
            int sum = 0;
            for (int i = 0; i < s.Length; i++)
            {
                int currentValue = dictionry[s[i]];
                if (i == s.Length - 1 || dictionry[s[i + 1]] <= currentValue)
                {
                    sum += currentValue;
                }
                else
                {
                    sum -= currentValue;
                }
            }
            return sum;
        }


    }
}
